<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/1/22
 * Time: 16:29
 */
namespace app\admin\controller;
use think\Db;
class Person extends  Base {

    /***
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where['sup_type'] = 1;
            if(isset($params['sup_status']) && $params['sup_status'] != '')
            {
                $where['sup_status'] = $params['sup_status'];
            }
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['sup_name|sup_phone'] = ['like', "%" . $params['key'] . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('super')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('super')
                ->where($where)
                ->order('sup_status ASC,sup_time DESC')
                ->page($Nowpage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['sup_status'] ==1){
                    $vo['sup_status_name'] = '审核中';
                }elseif ($vo['sup_status'] ==2){
                    $vo['sup_status_name'] = '同意';
                }elseif ($vo['sup_status'] ==3){
                    $vo['sup_status_name'] = '拒绝';
                }
                if($vo['is_super']==1){
                    $vo['super_name'] = '普通达人';
                }else{
                    $vo['super_name'] = '超级达人';
                }
                $realm_name = Db::table('up_realm')->where('realm_id','IN',$vo['sup_goods'])->column('realm_name');
                $vo['realm_name'] = implode(',',$realm_name);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /****
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_super')->where('sup_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /****
     * @return \think\response\Json
     * 审核不通过
     */
    public function refuse(){
        $id = input('id');
        $res = Db::table('up_super')->where('sup_id', $id)
            ->update(['sup_status' => 3 ,'sup_goods' => '']);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 审核通过
     */
    public function adopt(){
        $id = input('id');
        $be = Db::table('up_super')->where('sup_id', $id)->value('is_super');
        $res = Db::table('up_super')->where('sup_id', $id)
            ->update(['sup_status'=> 2,'sup_auth'=> $be]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /****
     * @return mixed
     * 申请达人详情信息
     */
    public function detail(){
        $id = input('param.id');
        $res =  Db::table('up_super')->where('sup_id',$id)->find();
        if($res['sup_image']!=''){
            $res['sup_image'] = explode(',',$res['sup_image']);
        }
        $realm_name = Db::table('up_realm')->where('realm_id','IN',$res['sup_goods'])->column('realm_name');
        $res['realm_name'] = implode(',',$realm_name);
        $this->assign('res',$res);
        return $this->fetch();
    }
}